<template>
  <div ref="pie"
       class="charts chart-pie"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
echarts.registerTheme('tdTheme', tdTheme)
export default {
    name: 'ChartPie',
    data () {
        return {
            chart_dom: null

        }
    },
    props: {
        data: Array
    },
    mounted () {
        this.$nextTick(() => {
            this.init()
        })
    },
    methods: {
        init () {
            this.chart_dom = echarts.init(this.$refs.pie)
            let option = {
                title: {
                    text: '对象分类数据量统计',
                    x: 'center',
                    padding: 15,
                    textStyle: {
                        color: '#28478d'
                    }

                },
                backgroundColor: '#f0f0f0',
                tooltip: {
                    trigger: 'item',
                    formatter: '{b} : {c} '
                },
                legend: {
                    orient: 'vertical',
                    left: 'right',
                    top: '25%',
                    data: this.data.map((item) => {
                        return item.name
                    })
                },
                series: [
                    { center: ['50%', '56%'],
                        type: 'pie',
                        avoidLabelOverlap: false,
                        label: {
                            formatter: '{d}%'
                        },
                        data: this.data,
                        label: {
                            formatter: ' {b|{b}：}{c}  {per|{d}%}  ',
                            fontSize: 12,
                            backgroundColor: '#eee',
                            borderColor: '#aaa',
                            borderWidth: 1,
                            borderRadius: 4,
                            rich: {
                                b: {
                                    fontSize: 16,
                                    lineHeight: 33
                                },
                                per: {
                                    color: '#eee',
                                    backgroundColor: '#334455',
                                    padding: [2, 4],
                                    borderRadius: 2
                                }
                            }
                        },
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            }
            this.chart_dom.setOption(option)
        },
        resize () {
            this.$nextTick(() => {
                this.chart_dom.resize()
            })
        }

    }
}
</script>

<style lang="less">
.chart-pie {
	height: 100%;
}
</style>
